//
//  ViewController.m
//  04_视觉效果
//
//  Created by apple on 16/5/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *backView;

@property (weak, nonatomic) IBOutlet UIView *layerView1;

@property (weak, nonatomic) IBOutlet UIView *purpleView;

@property (weak, nonatomic) IBOutlet UIView *layerView2;

@property (weak, nonatomic) IBOutlet UIView *layerView3;

@property (weak, nonatomic) IBOutlet UIView *maskView;

//将连续的view一起拖进来 , 定义成一个数组.
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *views;

@property (weak, nonatomic) IBOutlet UIView *layerView4;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self atrributeOfConrnerRadius];
    
    [self atrributeOfBorderWidth];
    
    [self atrributeOfShadowOpacity];
    
    [self atrributeOfMask];
    
    [self atrributeOfmagnificationFilter];
    
    [self atrributeOfShouldRasterize];

}


/**圆角(ConrnerRadius)*/
- (void)atrributeOfConrnerRadius{

    //ConrnerRadius 属性 图层角的曲率(0 为直角)
    //默认情况下 只能影响背景颜色 与 不能影响背景图片与子视图.
    //但与 masksToBounds(裁边) 一起使用 , 就可以将所有东西全部截取.
    
    self.layerView1.layer.cornerRadius = 20;
    self.layerView1.layer.masksToBounds = YES;


}

/** 图层边框(BorderWidth && borderColor) */
- (void)atrributeOfBorderWidth{

    //BorderWidth 边框的粗细 (默 0)
    //BorderColor 边框的颜色 (默 黑色)
    //边框是 绘制在图层边界里面的.
    
    self.layerView1.layer.borderWidth = 10.0f;
    self.layerView1.layer.borderColor = [UIColor redColor].CGColor;

    self.layerView1.layer.masksToBounds = YES;
    
}


/**阴影 (shadowOpacity)*/
- (void)atrributeOfShadowOpacity{

    //shadowOpacity 阴影(0(默) ~ 1)的值 0为不显示, 1为不透明.
    //shadowColor   阴影颜色. (默 黑色)
    //shadowOffset  阴影偏移量. (默 {0.-3})正数为向左, 向下
    //shadowRadius  阴影的模糊度.
    // 图层的阴影继承自内容的外形.CA会将寄宿图考虑在内.

    UIImage *image = [UIImage imageNamed:@"tupian"];
    
    self.layerView2.layer.contents = (__bridge id)image.CGImage;
    
    self.layerView2.layer.shadowOpacity = 1;
    self.layerView2.layer.shadowOffset = CGSizeMake(0, 10);
    self.layerView2.layer.shadowRadius = 10;
    
    
    
    //maskToBounds 会裁剪掉阴影的内容 .
    self.layerView1.layer.shadowOpacity = 0.5;
    self.layerView1.layer.shadowOffset = CGSizeMake(0, 10);
    self.layerView1.layer.shadowRadius = 10;
    
    
    //想阴影 , 圆角同时出现 . 可以嵌套一个父视图 , 父视图做阴影, 子视图做圆角.
    //UIView 上不能添加颜色!
    self.backView.layer.shadowOpacity = 0.5;
    self.backView.layer.shadowOffset = CGSizeMake(0, 10);
    self.backView.layer.shadowRadius = 10;
    
    
    
    //shadowPath 指定任意阴影形状.

    self.layerView3.layer.contents = (id)image.CGImage;
    
    self.layerView3.layer.shadowOpacity = 0.5;
    
    CGMutablePathRef squarepath = CGPathCreateMutable();
    //方形阴影
//    CGPathAddRect(squarepath, NULL, self.layerView3.bounds);
    //圆形阴影
    CGPathAddEllipseInRect(squarepath, NULL, self.layerView3.bounds);
    
    self.layerView3.layer.shadowPath = squarepath;
    CGPathRelease(squarepath);

}



/**图层蒙版 (mask)*/

- (void)atrributeOfMask{

    
    //可以通过mask 图层的轮廓 ,来截取当前View的形状.
    //mask图层实心的部分会被保留下来，其他的则会被抛弃。
    //mask 不止局限于静态区.
    CALayer *layer = [CALayer layer];
    
    UIImage *image = [UIImage imageNamed:@"mask"];
    layer.frame = self.maskView.bounds;
    layer.contents = (id)image.CGImage;
    
    self.maskView.layer.mask = layer;
    
}



/**拉伸过滤 (magnificationFilter && minificationFilter)*/

- (void)atrributeOfmagnificationFilter{

    //当图片需要显示不同的大小的时候,有一种拉伸过滤的算法.
    //作用于原图的像素上, 并根据需要 生成新的像素显示在屏幕上.
    //算法:kCAFilterLinear (默认过滤器) (双线性滤波算法)
    //    kCAFilterNearest (最近过滤)(适用于 比较小的图 ,或差异特别明显,极少斜线的大图)
    //    kCAFilterTrilinear (三线性滤波算法)
    // 线性过滤保留了形状, 最近过滤保留了像素的差异.
    
    
    UIImage *image = [UIImage imageNamed:@"time"];

    for (UIView *view in self.views) {
        
        view.layer.contents = (id)image.CGImage;
        view.layer.contentsRect = CGRectMake(0, 0, 0.1, 1);
        view.layer.contentsGravity = kCAGravityResizeAspect;
        //最近过滤
        view.layer.magnificationFilter = kCAFilterNearest;
    }
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
    
    [self changeTime];
    
}


- (void)changeTime{

    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSUInteger units = NSCalendarUnitHour|NSCalendarUnitMinute| NSCalendarUnitSecond;
    
    NSDateComponents *component = [calendar components:units fromDate:[NSDate date]];
    
    ((UIView *)self.views[0]).layer.contentsRect =
                                CGRectMake((component.hour/10)*0.1, 0, 0.1, 1);
    ((UIView *)self.views[1]).layer.contentsRect =
                                CGRectMake((component.hour%10)*0.1, 0, 0.1, 1);
    ((UIView *)self.views[2]).layer.contentsRect =
                                CGRectMake((component.minute/10)*0.1, 0, 0.1, 1);
    ((UIView *)self.views[3]).layer.contentsRect =
                                CGRectMake((component.minute%10)*0.1, 0, 0.1, 1);
    ((UIView *)self.views[4]).layer.contentsRect =
                                CGRectMake((component.second/10)*0.1, 0, 0.1, 1);
    ((UIView *)self.views[5]).layer.contentsRect =
                                CGRectMake((component.second%10)*0.1, 0, 0.1, 1);
    
}


/**组透明 (shouldRasterize)*/

- (void)atrributeOfShouldRasterize{

    //opacity : 确定Layer层视图的透明度 (与alpha 类似) -> 会影响子层级.
    //shouldRasterize : 在应用透明度之前 , 图层及其子图层都会被整合成一个整体的图片.
    
    UIButton *button1 = [self createButton];
    button1.center = CGPointMake(50, 30);
    [self.layerView4 addSubview:button1];
    button1.layer.opacity = 0.5;
    
    
    UIButton *button2 = [self createButton];
    button2.center = CGPointMake(160, 30);
    button2.layer.opacity = 0.5;
    [self.layerView4 addSubview:button2];
    
    button2.layer.shouldRasterize = YES;
    button2.layer.contentsScale = [UIScreen mainScreen].scale;
    
    button1.layer.allowsGroupOpacity = YES;
    
    
    
}

- (UIButton *)createButton{

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    button.backgroundColor = [UIColor whiteColor];
    button.layer.cornerRadius = 10;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 60, 20)];
    label.text = @"组透明";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor whiteColor];
    [button addSubview:label];

    return button;

}





@end
